package com.core.life.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.core.common.controller.BaseController;
import com.core.common.dao.Pager;
import com.core.common.pageModel.EasyuiDataGridJson;
import com.core.common.pageModel.JsonMessage;
import com.core.life.service.IConsumeRecordService;
import com.core.life.vo.ConsumeRecordVo;

@Controller
@RequestMapping("admin/consumeRecord")
public class ConsumeRecordController extends BaseController {

    @Resource
    private IConsumeRecordService consumeRecordService;

    /**
     * 跳转到日常消费
     */
    @RequestMapping("/everyConsume/index")
    public String everyConsumeIndex() {
        return "admin/everyConsume/index";
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public EasyuiDataGridJson getList(int page, int rows, String sort, String order,
            HttpServletRequest request) {
        EasyuiDataGridJson json = new EasyuiDataGridJson();
        String strDate = request.getParameter("countDatetime");
        Map<String, Object> optionMap = null;
        if (StringUtils.isNotBlank(strDate)) {
            optionMap = new HashMap<String, Object>();
            optionMap.put("strDate", strDate);
        }
        Pager<ConsumeRecordVo> pager = consumeRecordService.getList(page, rows, sort, order, optionMap);
        if (pager != null) {
            json.setTotal(pager.getTotalCount());
            json.setRows(pager.getDatas());
        }
        if (null == json.getRows()) {
            json.setTotal(0l);
            json.setRows(new ArrayList<ConsumeRecordVo>());
        }
        return json;
    }

    @RequestMapping("/toCreateEveryConsume/index")
    public String toCreateIndex() {
        return "admin/everyConsume/create";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage create(ConsumeRecordVo vo) {
        JsonMessage jsonMsg = new JsonMessage();
        try {
            consumeRecordService.save(vo);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsg.setSuccess(false);
            jsonMsg.setMsg("添加记录出错！");
            jsonMsg.setObj(e.getMessage());
            return jsonMsg;
        }
        jsonMsg.setSuccess(true);
        jsonMsg.setMsg("添加记录成功！");
        return jsonMsg;
    }

    @RequestMapping("/toEditEveryConsume/index")
    public String toEditIndex(Long id, Model model) {
        model.addAttribute("consumeRecordId", id);
        return "admin/everyConsume/edit";
    }

    @RequestMapping(value = "/getconsumeRecordForm", method = RequestMethod.POST)
    @ResponseBody
    public ConsumeRecordVo getForm(Long id) {
        return consumeRecordService.getById(id);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage edit(ConsumeRecordVo vo) {
        JsonMessage jsonMsg = new JsonMessage();
        try {
            consumeRecordService.update(vo);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsg.setSuccess(false);
            jsonMsg.setMsg("修改记录出错！");
            jsonMsg.setObj(e.getMessage());
            return jsonMsg;
        }
        jsonMsg.setSuccess(true);
        jsonMsg.setMsg("修改记录成功！");
        return jsonMsg;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage delete(String ids) {
        JsonMessage jsonMsg = new JsonMessage();
        try {
            consumeRecordService.delete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMsg.setSuccess(false);
            jsonMsg.setMsg("删除记录出错！");
            jsonMsg.setObj(e.getMessage());
            return jsonMsg;
        }
        jsonMsg.setSuccess(true);
        jsonMsg.setMsg("删除记录成功！");
        return jsonMsg;
    }
}
